package com.chuanyang.driverh.mainhome.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.mainhome.OrderDetailRceiveActivity;
import com.chuanyang.driverh.mainhome.WaybilDetailDirverActivity;
import com.chuanyang.driverh.response2.ResultWaitTransDriver;
import com.chuanyang.driverh.utils.StringUtil;


public class WaitTransDriverAdapter extends BaseListAdapter<ResultWaitTransDriver.OutDataEntity.DataEntity.PageWayBillListEntity> {
    //    private ViewHolder viewHolder;
    private Activity activity;
    private Dialog dialog;

    public WaitTransDriverAdapter(Activity activity) {
        super(activity);//此处不传入集合，则默认集合长度为0，在setdataresult（）方法中，添加数据并刷新
        this.activity = activity;
    }



    @Override
    public View getZkView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = getInflater().inflate(R.layout.home_item_wait_trans_driver_top, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.tv_orgin = (TextView) convertView.findViewById(R.id.tv_orgin);
            viewHolder.tv_shifa = (TextView) convertView.findViewById(R.id.tv_shifa);
            viewHolder.tv_order_status = (TextView) convertView.findViewById(R.id.tv_order_status);
            viewHolder.tv_order_no = (TextView) convertView.findViewById(R.id.tv_order_no);
            viewHolder.tv_order_time = (TextView) convertView.findViewById(R.id.tv_order_time);
            viewHolder.tv_city_start = (TextView) convertView.findViewById(R.id.tv_city_start);
            viewHolder.tv_city_end = (TextView) convertView.findViewById(R.id.tv_city_end);
//            viewHolder.tv_weight = (TextView) convertView.findViewById(R.id.tv_weight);
//            viewHolder.tv_weight_unit = (TextView) convertView.findViewById(R.id.tv_weight_unit);
//            viewHolder.tv_num = (TextView) convertView.findViewById(R.id.tv_num);
//            viewHolder.tv_num_unit = (TextView) convertView.findViewById(R.id.tv_num_unit);

            viewHolder.tv_total_price = (TextView) convertView.findViewById(R.id.tv_total_price);
            viewHolder.tv_act2 = (TextView) convertView.findViewById(R.id.tv_act2);
            viewHolder.tv_order_type = (TextView) convertView.findViewById(R.id.tv_order_type);
            viewHolder.iv_left = (ImageView) convertView.findViewById(R.id.iv_left);
            viewHolder.vw_line2 = (View) convertView.findViewById(R.id.vw_line2);
            viewHolder.vw_line1 = (View) convertView.findViewById(R.id.vw_line1);
//            viewHolder.iv_car_or_ship = (ImageView) convertView.findViewById(R.id.iv_car_or_ship);
            viewHolder.tv_waybill_no = (TextView) convertView.findViewById(R.id.tv_waybill_no);
            viewHolder.tv_act1 = (TextView) convertView.findViewById(R.id.tv_act1);
            viewHolder.ll_container = (LinearLayout) convertView.findViewById(R.id.ll_container);
            viewHolder.ll_order_div1 = (LinearLayout) convertView.findViewById(R.id.ll_order_div1);
            viewHolder.ll_div = (LinearLayout) convertView.findViewById(R.id.ll_div);
            viewHolder.tv_notice = (TextView) convertView.findViewById(R.id.tv_notice);
            viewHolder.tv_notice_no = (TextView) convertView.findViewById(R.id.tv_notice_no);
//            viewHolder.tv_shifa_gua = (TextView) convertView.findViewById(R.id.tv_shifa_gua);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();

        }
        final ResultWaitTransDriver.OutDataEntity.DataEntity.PageWayBillListEntity data = getItem(position);

        String currency="¥";
        if (data.getCurrency() == 1) {
            currency="¥";
            viewHolder.tv_total_price.setText("总价" + StringUtil.getDouble(data.getSumPrice() + "") + "￥");
        } else {
            currency="$";
            viewHolder.tv_total_price.setText("总价" + StringUtil.getDouble(data.getSumPrice() + "") + "$");

        }
        viewHolder.tv_city_start.setText(data.getStartPoint());
        viewHolder.tv_city_end.setText(data.getEndPoint());
//        viewHolder.tv_weight.setText(StringUtil.getDouble(data.getSumWeight() + ""));
//        viewHolder.tv_num.setText(data.getSumNum() + "");

        viewHolder.tv_waybill_no.setText(data.getWaybillNo() + "");
        viewHolder.tv_order_no.setText(data.getOrderNo());
        viewHolder.tv_order_time.setText(data.getOrderTime());
        if (data.getObjType() == 1) {//个人接单
            viewHolder.ll_order_div1.setVisibility(View.VISIBLE);
            setTypeView(data, viewHolder.tv_order_type);
            viewHolder.vw_line1.setVisibility(View.VISIBLE);
            setBillStatus(viewHolder, data);
            setOrderStatusDetail(viewHolder.tv_order_status,data);


        } else if (data.getObjType() == 2) {//分配来的第一个单
            viewHolder.ll_order_div1.setVisibility(View.VISIBLE);
            setTypeView(data, viewHolder.tv_order_type);
            setBillStatus(viewHolder, data);
            setOrderStatus(viewHolder.tv_order_status,data);
            viewHolder.vw_line1.setVisibility(View.VISIBLE);

        } else {////分配来的不是第一个单
            viewHolder.ll_order_div1.setVisibility(View.GONE);
            setTypeView(data, viewHolder.tv_order_type);
            setBillStatus(viewHolder, data);
            viewHolder.vw_line1.setVisibility(View.GONE);
        }
            viewHolder.tv_notice.setVisibility(View.GONE);

//            if (data.getWaybillStatus() <= 3) {
//                viewHolder.iv_left.setImageResource(R.mipmap.driver_before_load);
//            } else if (data.getWaybillStatus() < 6) {
//                viewHolder.iv_left.setImageResource(R.mipmap.driver_transing);
//            } else if (data.getWaybillStatus() == 6) {
//                viewHolder.iv_left.setImageResource(R.mipmap.driver_before_get_goods);
//            } else if (data.getWaybillStatus() == 7) {
//                viewHolder.iv_left.setImageResource(R.mipmap.driver_has_complete);
//            }
        viewHolder.tv_act1.setEnabled(true);

        if (StringUtil.isBlank(data.getNoticeNo())){
            viewHolder.tv_notice_no.setVisibility(View.GONE);
        }else {
            viewHolder.tv_notice_no.setVisibility(View.VISIBLE);
            if (data.getNoticeNo().contains(",")){
                viewHolder.tv_notice_no.setText(data.getNoticeNo().substring(0,data.getNoticeNo().indexOf(",")-1)+"...");
            }else {
                viewHolder.tv_notice_no.setText(data.getNoticeNo());
            }
        }

        if (data.getWaybillStatus()<4)
        {
            if (data.getApplyNoStatus() == 1) {
                viewHolder.tv_notice.setText("申请放弃运单中");
                viewHolder.tv_notice.setVisibility(View.VISIBLE);
                viewHolder.tv_act1.setVisibility(View.INVISIBLE);
//                viewHolder.tv_act1.setText("申请中");
//                viewHolder.tv_act1.setEnabled(false);
            } else if (data.getApplyNoStatus() == 2) {
                viewHolder.tv_notice.setText("队长拒绝");
                viewHolder.tv_notice.setVisibility(View.VISIBLE);
                viewHolder.tv_act1.setText("申请放弃");
                viewHolder.tv_act1.setEnabled(false);
                viewHolder.tv_act1.setVisibility(View.INVISIBLE);
            } else {
                viewHolder.tv_notice.setVisibility(View.GONE);
            }
        }
        final ViewHolder finalViewHolder = viewHolder;

        viewHolder.ll_order_div1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(activity, OrderDetailRceiveActivity.class);
                intent.putExtra("data",data.getOrderId()+"");
                intent.putExtra("type","0");
                activity.startActivity(intent);
            }
        });

        viewHolder.tv_act1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //如果申请过放弃，
                if (data.getWaybillStatus()<3 && data.getApplyNoStatus()!=0){
                    return;
                }
                if (listener != null) {
                    listener.onCustomerListener(finalViewHolder.tv_act1, position);
                }
            }
        });
        viewHolder.tv_act2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onCustomerListener(finalViewHolder.tv_act2, position);
                }
            }
        });
        viewHolder.ll_div.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            /*    if (data.getFleetId()!=0){
                    Intent intent=new Intent(activity, WaybilDetailDirverActivity.class);
                    intent.putExtra("waybilId",data.getWaybillId()+"");
                    intent.putExtra("data",data);
                    if (data.getOrderType()==1){//个人接的单子
                        intent.putExtra("type", 2);
                    }else {//车队长分配的单子。
                        intent.putExtra("type", 1);
                    }

                    activity.startActivity(intent);
                }else {
                    Intent intent=new Intent(activity, WaybilDetailActivity.class);
                    intent.putExtra("waybilId",data.getWaybillId()+"");
                    activity.startActivity(intent);
                }*/
                    Intent intent=new Intent(activity, WaybilDetailDirverActivity.class);
                    intent.putExtra("waybilId",data.getWaybillId()+"");
                    intent.putExtra("data",data);
                    if (data.getFleetId()!=0){//车队长分配的单子。
                        intent.putExtra("type", 1);
                    }else {//个人接的单子
                        intent.putExtra("type", 2);
                    }
                    activity.startActivity(intent);



            }
        });


//5.4日改成不显示。
        viewHolder. tv_total_price.setVisibility(View.VISIBLE);
        viewHolder.tv_total_price.setText("单价:" + StringUtil.getDouble(data.getPrice() + "") + currency+"/t");

        return convertView;
    }
   private  void setOrderStatus(TextView tv_order_status, ResultWaitTransDriver.OutDataEntity.DataEntity.PageWayBillListEntity data)
   {
       if(data.getOrderStatus()<=2)
       {
           tv_order_status.setText("待装货");
           tv_order_status.setTextColor(activity.getResources().getColor(R.color.text_assign_car_gray));

       }
       else if(data.getOrderStatus()>=3&&data.getOrderStatus()<6)
       {
          tv_order_status.setText("运输中");
           tv_order_status.setTextColor(activity.getResources().getColor(R.color.text_assign_car_green));

       }
       else if(data.getOrderStatus()==6)
       {
          tv_order_status.setText("待收货");
           tv_order_status.setTextColor(activity.getResources().getColor(R.color.text_get_order_orange));

       }
       else {
          tv_order_status.setText("已完成");
           tv_order_status.setTextColor(activity.getResources().getColor(R.color.green));

       }
   }
    private  void setOrderStatusDetail(TextView tv_order_status, ResultWaitTransDriver.OutDataEntity.DataEntity.PageWayBillListEntity data)
    {
        if(data.getOrderStatus()<=2)
        {
            tv_order_status.setText("待装货");
            tv_order_status.setTextColor(activity.getResources().getColor(R.color.text_assign_car_gray));
        }
        else if(data.getOrderStatus()==3)
        {
            tv_order_status.setText("装货中");
            tv_order_status.setTextColor(activity.getResources().getColor(R.color.text_assign_car_gray));
        }
        else if(data.getOrderStatus()==4)
        {
            tv_order_status.setText("运输中");
            tv_order_status.setTextColor(activity.getResources().getColor(R.color.text_assign_car_green));
        }
        else if(data.getOrderStatus()==5)
        {
            tv_order_status.setText("卸货中");
            tv_order_status.setTextColor(activity.getResources().getColor(R.color.text_assign_car_green));
        }
        else if(data.getOrderStatus()==6)
        {
            tv_order_status.setText("待收货");
            tv_order_status.setTextColor(activity.getResources().getColor(R.color.text_get_order_orange));
        }
        else {
            tv_order_status.setText("已完成");
            tv_order_status.setTextColor(activity.getResources().getColor(R.color.green));
        }
    }
    private void setBillStatus(ViewHolder viewHolder, ResultWaitTransDriver.OutDataEntity.DataEntity.PageWayBillListEntity data) {
        viewHolder.vw_line2.setVisibility(View.VISIBLE);
        if (data.getWaybillStatus() <= 2) {
            viewHolder.tv_orgin.setVisibility(View.GONE);
            viewHolder.tv_shifa.setText("挂牌:"+StringUtil.getDouble(data.getOriginalWeight() + "") + "t/" + data.getOriginalNum() + "件");

            viewHolder.ll_container.setVisibility(View.VISIBLE);
            viewHolder.tv_order_status.setText("待装货");
            viewHolder.iv_left.setImageResource(R.mipmap.driver_before_load);
            viewHolder.tv_act1.setVisibility(View.VISIBLE);
            viewHolder.tv_act2.setText("开始运输");
            viewHolder.tv_act2.setVisibility(View.VISIBLE);
            if (data.getFleetId() != 0) {
                if (data.getApplyNoStatus()==1){
                    viewHolder.tv_act1.setText("已申请");
                }else if(data.getApplyNoStatus()==2){
                    viewHolder.tv_act1.setText("司机拒绝");
                }else {
                    viewHolder.tv_act1.setText("申请放弃");
                }
                viewHolder.tv_act1.setVisibility(View.VISIBLE);
            } else {
                viewHolder.tv_act1.setVisibility(View.GONE);
            }


        }
        else if (data.getWaybillStatus() == 3) {
            viewHolder.tv_orgin.setVisibility(View.GONE);
            viewHolder.tv_shifa.setText("挂牌:"+StringUtil.getDouble(data.getOriginalWeight() + "") + "t/" + data.getOriginalNum() + "件");
            viewHolder.ll_container.setVisibility(View.VISIBLE);
            viewHolder.tv_order_status.setText("装货中");
            viewHolder.iv_left.setImageResource(R.mipmap.driver_before_load);
            viewHolder.tv_act1.setText("装货向导");
            viewHolder.tv_act1.setVisibility(View.VISIBLE);
            viewHolder.tv_act2.setVisibility(View.GONE);


        } else if (data.getWaybillStatus() == 4) {
            viewHolder.tv_shifa.setText("实发:"+StringUtil.getDouble(data.getSumWeight() + "") + "t/" + data.getSumNum() + "件");
            viewHolder.tv_orgin.setVisibility(View.VISIBLE);
            viewHolder.tv_orgin.setText("挂牌:"+StringUtil.getDouble(data.getOriginalWeight() + "") + "t/" + data.getOriginalNum() + "件");
            viewHolder.tv_orgin.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            viewHolder.ll_container.setVisibility(View.VISIBLE);
            viewHolder.tv_order_status.setText("运输中");
            viewHolder.iv_left.setImageResource(R.mipmap.driver_transing);
            viewHolder.tv_act2.setVisibility(View.VISIBLE);
            viewHolder.tv_act2.setText("运输完成");
            //如果是车显示导航 如果是船则隐藏
            if (data.getTransPortWay() == 1)//车
            {
                viewHolder.tv_act1.setText("导航");
                viewHolder.tv_act1.setVisibility(View.VISIBLE);
            } else if (data.getTransPortWay() == 2) {
                viewHolder.tv_act1.setVisibility(View.GONE);

            }


        } else if (data.getWaybillStatus() == 5) {
            viewHolder.ll_container.setVisibility(View.VISIBLE);
            viewHolder.tv_shifa.setText("实发:"+StringUtil.getDouble(data.getSumWeight() + "") + "t/" + data.getSumNum() + "件");
            viewHolder.tv_shifa.setText("实发:"+StringUtil.getDouble(data.getSumWeight() + "") + "t/" + data.getSumNum() + "件");
            viewHolder.tv_orgin.setVisibility(View.VISIBLE);
            viewHolder.tv_orgin.setText("挂牌:"+StringUtil.getDouble(data.getOriginalWeight() + "") + "t/" + data.getOriginalNum() + "件");
            viewHolder.tv_orgin.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            viewHolder.tv_order_status.setText("卸货中");
            viewHolder.iv_left.setImageResource(R.mipmap.driver_transing);
            viewHolder.tv_act1.setText("卸货向导");
            viewHolder.tv_act1.setVisibility(View.VISIBLE);
            viewHolder.tv_act2.setVisibility(View.VISIBLE);
            viewHolder.tv_act2.setText("运输完成");


        } else if (data.getWaybillStatus() == 6) {
            viewHolder.ll_container.setVisibility(View.VISIBLE);
            viewHolder.tv_shifa.setText("实发:"+StringUtil.getDouble(data.getSumWeight() + "") + "t/" + data.getSumNum() + "件");
            viewHolder.tv_orgin.setVisibility(View.VISIBLE);
            viewHolder.tv_orgin.setText("挂牌:"+StringUtil.getDouble(data.getOriginalWeight() + "") + "t/" + data.getOriginalNum() + "件");
            viewHolder.tv_orgin.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            viewHolder.tv_order_status.setText("待收货");
            viewHolder.iv_left.setImageResource(R.mipmap.driver_before_get_goods);
            viewHolder.tv_act1.setText("验证卸货");
            viewHolder.tv_act1.setVisibility(View.VISIBLE);
            viewHolder.tv_act2.setVisibility(View.GONE);
        }
        else {
            viewHolder.vw_line2.setVisibility(View.GONE);
            viewHolder.tv_shifa.setText("实发:"+StringUtil.getDouble(data.getSumWeight() + "") + "t/" + data.getSumNum() + "件");
            viewHolder.tv_orgin.setVisibility(View.VISIBLE);
            viewHolder.tv_orgin.setText("挂牌:"+StringUtil.getDouble(data.getOriginalWeight() + "") + "t/" + data.getOriginalNum() + "件");
            viewHolder.tv_orgin.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            viewHolder.tv_order_status.setText("已完成");
            viewHolder.iv_left.setImageResource(R.mipmap.driver_has_complete);
//            viewHolder.ll_container.setVisibility(View.GONE);
            viewHolder.ll_container.setVisibility(View.VISIBLE);
            viewHolder.tv_act1.setVisibility(View.GONE);
            viewHolder.tv_act2.setVisibility(View.GONE);
        }
    }

    private void setTypeView(ResultWaitTransDriver.OutDataEntity.DataEntity.PageWayBillListEntity data, TextView view) {
        if (data.getOrderType() == 1) {//1竞价
            if (data.getFleetId() != 0) {
                view.setBackgroundResource(R.mipmap.order_bid_t);
            } else {
                view.setBackgroundResource(R.mipmap.order_bid_p);
            }

        } else if (data.getOrderType() == 2) {//匹配
            if (data.getFleetId() != 0) {
                view.setBackgroundResource(R.mipmap.order_match_t);
            } else {
                view.setBackgroundResource(R.mipmap.order_match_p);
            }

        } else {//专线
            if (data.getFleetId() != 0) {
                view.setBackgroundResource(R.mipmap.order_special_t);
            } else {
                view.setBackgroundResource(R.mipmap.order_special_p);
            }
        }

    }


    class ViewHolder {


        TextView tv_order_status;
        TextView tv_order_no;
        TextView tv_order_time;
        ImageView iv_left;
        TextView tv_city_start;
        TextView tv_city_end;
//        TextView tv_weight;
//        TextView tv_weight_unit;
//        TextView tv_num;
//        TextView tv_num_unit;
        TextView tv_waybill_no;
        TextView tv_total_price;
        TextView tv_order_type;
        TextView tv_act1;
        TextView tv_act2;
        LinearLayout ll_container;
        LinearLayout ll_order_div1;
        LinearLayout ll_div;
        View vw_line2;
        View vw_line1;
        TextView tv_orgin;
        TextView tv_notice;
        TextView tv_notice_no;
        TextView tv_shifa;

    }
}
